/*
 * Merges dataset xml into one harvestable file for Fedora
 */

package monash.med.crystallography.DatasetTools.oai_dsMerge;

import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
import datasets.jaxb.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.xml.sax.SAXParseException;

/**
 *
 * @author steve
 */
public class oai_dsMerge extends NamespacePrefixMapper {

    private Datasets datasets;

    /* run through directory and add all datasets to jaxb dataset objects before
        rewriting the xml */
    public oai_dsMerge(String dir) 
    {
        try{
            JAXBContext jaxbContext = JAXBContext.newInstance("datasets.jaxb");
        }
        catch(JAXBException j)
        {
            
        }
        ObjectFactory objFactory = new ObjectFactory();  
                
        datasets = objFactory.createDatasets();    
        
        for(java.io.File file : new java.io.File(dir).listFiles())
        {    
            if(file.isFile() && file.getName().endsWith(".xml"))
            {   
                try{
                    for(Datasets.Dataset dataset : addDatasets(file.getAbsolutePath()))
                    {
                        datasets.getDataset().add(dataset);
                    }
                }
                catch(SAXParseException s)
                {
                    
                }
            }
        }
    }

    private ArrayList<Datasets.Dataset> addDatasets(String filePath) throws SAXParseException
    {
        ArrayList<Datasets.Dataset> datasetList = new ArrayList<Datasets.Dataset>();
        
        try {
            
            java.io.File file = new java.io.File(filePath);
            JAXBContext jc = JAXBContext.newInstance("datasets.jaxb");
            Unmarshaller unmarshaller = jc.createUnmarshaller();

            Datasets localDatasets= (Datasets)
                     unmarshaller.unmarshal(file);

            datasetList = (ArrayList)localDatasets.getDataset();
            
            file.delete();
            
            return datasetList;
        }
        catch(JAXBException j)
        {
            
        }
        
        return datasetList;
    }
    
    public String writeOai_ds(String writeDir)
    {
        try {
            JAXBContext jc = JAXBContext.newInstance("datasets.jaxb");
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT ,
                   new Boolean(true));             
            marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", this);
            marshaller.marshal(datasets,
            new FileOutputStream(writeDir)); 
            System.out.println("Wrote to " + writeDir);
            return writeDir;
        }
        catch(FileNotFoundException f)
        {
            f.printStackTrace();
        }    
        catch(JAXBException j)
        {
            j.printStackTrace();
        }
        return "";
    }
    
    @Override
    public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
        return "oai_ds";
    }    
            
}
